<?php
/**
 * 投票
 */
class Vote extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper('common');
        $this->load->model('activity_vote_model', 'activity_vote');
        $this->load->model('activity_vote_topic_model', 'activity_vote_topic');
        $this->load->model('activity_vote_user_model', 'activity_vote_user');
        $this->load->model('activity_vote_topic_option_model', 'activity_vote_topic_option');
    }

    public function index($voteId)
    {
        $vote = $this->activity_vote->findById($voteId);
        if ( $vote->num_rows() == 0 ) {
            $this->alertJumpPre('投票活动不存在');
        }
        $data['vote'] = $vote->row();
        $uid = $this->uid ? $this->uid : -1; // 查找不到
        $activity = $this->isVote(getCip(), $voteId,$uid);
        if ( $activity ) {
            $data['isUse'] = $activity;//$activity;
        }
        $result = $this->activity_vote_topic->findVoteTopicOption($voteId);
        if ( $result->num_rows() == 0 ) {
            $this->alertJumpPre('投票活动没有添加选项');
        }
        $voteTopic = array();
        foreach ( $result->result() as $key=>$value ) {
            $voteTopic[$value->topic_id]['title'] = $value->title;
            if ( isset($voteTopic[$value->topic_id]['voteTotal']) ) {
                $voteTopic[$value->topic_id]['voteTotal'] += $value->count;
            } else {
                $voteTopic[$value->topic_id]['voteTotal'] = $value->count;
            }
            $voteTopic[$value->topic_id]['optionList'][] = $value;
        }
        $data['voteId'] = $voteId;
        $data['voteTopic'] = $voteTopic;
        $this->toVoteView($voteId,$data);
        
    }

    public function votePost()
    {
        $postData = $this->input->post();
        $vote = $this->activity_vote->findById($postData['vote_id']);
        if ( $vote->num_rows() == 0 ) {
            $this->alertJumpPre('投票活动不存在');
        }
        $vote = $vote->row();

        $now = date('Y-m-d H:i:s');
        if ($vote->start_date < $now || $now >$vote->end_date) {
            $this->alertJumpPre('活动已结束');
        }
        $ip = getCip();
        $uid = $this->uid ? $this->uid : -1; // 查找不到
        /*
        $vote_number = get_cookie('vote_number') ? get_cookie('vote_number') : sha1(md5('bzezt_1'));
        if ($vote_number != $postData['vote_number']) {
            $this->alertJumpPre('您今天已投票了');
        }
        */
        if ( $this->isVote($ip, $postData['vote_id'],$uid) ) {
            $this->alertJumpPre('您今天已投票了');
        }
        $optionIds = explode(',', trim($postData['option_id'], ','));

        $result = $this->activity_vote_topic_option->findByIds($optionIds);
        if ( $result->num_rows() != count($optionIds) ){
            $this->alertJumpPre('投票失败,投票选项不存在');
        }

        $details = array();
        foreach( $result->result() as $value ) {
            $details[$value->id] = $value->option_name;
        }
        $postData['details'] = json_encode($details);
        $postData['ip'] = $ip;
        $postData['uid'] = $this->uid ? $this->uid : 0;
        $this->db->trans_start();
        $this->activity_vote_topic_option->updateCount($optionIds);
        $this->activity_vote_user->insertVoteUser($postData);
        $this->db->trans_complete();

        if ( $this->db->trans_status() == FALSE ) {
            $this->alertJumpPre('投票失败');
        } else {
        	//set_cookie('vote_number',sha1(md5('bzezt_2')),3*3600);
            $this->session->set_flashdata('vote', true);
            $this->redirect('home/vote/index/'. $postData['vote_id'].'?unfounder='.$this->input->post('unfounder'));
        }
    }
    
     /**
     * 页面的跳转
     * @param unknown $voteId
     * @param unknown $data
     */
    private function toVoteView($voteId,$data) {
    	
        switch ($voteId) {
        	case 4 : 
        	    $data['headTitle'] = '这个春节，你想要什么年货呢？升级版着想“金牌年货”评选再次来袭，“最受欢迎年货、最佳招待年货、最放心年货”等单品年货奖项，在2016新春年货给力降价';
        	    $this->load->view('home/vote/index', $data);
        	break;
        	case 7 :
        	    $data['headTitle'] = '优秀商家评选活动';
        	    $this->load->view('home/vote/sxvote', $data);
        	break;
        }
    }

    /**
     * 用户今天是否投票
     * @param $voteId
     * @return bool
     */
    private function isVote($ip, $voteId,$uid)
    {
        $result = $this->activity_vote_user->findByIp($ip, $voteId,$type=2,$uid);
        if ( $result->num_rows() == 0 ) {
            return false;
        }
        return true;
    }
}